let util = require('../../util/util')

const app = getApp();
Page({
    data: {
        videoList: [],
        receiveCurrent: null,
        indexCurrent: null,
        currentId: null,
        currentIdDetail: [],
        slipCurrent: null,
        slipStatus: false,
        collectText: '收藏',
    },
    onLoad: function (options) {
        this.setData({
            receiveCurrent: options.nowCurrentId
        })
        this.getVideoInfo();
        this.decideEveryOneVideoStatus();
        this.historyAdd();
    },
    onReady() {
        this.getVideoInfo();
    },
    getVideoInfo() {
        wx.showLoading({
            title: '加载中',
            mask: true,
        })
        wx.cloud.callFunction({
            name: 'getVideoFun',
            data: {
                fun_name: "get_all_video_list",
                collection_name: "videoData",
            },
            success: res => {
                wx.hideLoading();
                let videoList = res.result.data;
                if (videoList.length > 0) {
                    this.setData({
                        videoList: videoList
                    })
                } else {
                    wx.showToast({
                        title: '没有获取到数据',
                        duration: 2000,
                        mask: true
                    })
                }
            },
            fail: err => {
                util.errorModal(err);
            }
        })
    },
    playVideo(evt) {
        let currentIndex = evt.target.id;
        if (!this.data.indexCurrent) {
            this.setData({
                indexCurrent: currentIndex
            })
            let videoNowContext = wx.createVideoContext(this.data.indexCurrent);
            videoNowContext.play();
        } else {
            var videoContextPrev = wx.createVideoContext(this.data.indexCurrent);
            if (this.data.indexCurrent != currentIndex) {
                videoContextPrev.pause();
                videoContextPrev.seek(0);
                this.setData({
                    indexCurrent: currentIndex
                })
            }
        }
        this.historyAdd();
    },
    swiperChange(evt) {
        this.setData({
            slipStatus: true,
            slipCurrent: evt.detail.current,
            collectText: '收藏'
        })
        wx.cloud.callFunction({
            name: "updateVideoFun",
            data: {
                fun_name: 'update_video_look_num',
                collection_name: "videoData",
                _id: this.data.videoList[evt.detail.current]._id
            },
            fail: err => {
                util.errorModal(err);
            }
        });
        this.historyAdd();
        this.decideEveryOneVideoStatus();
    },
    bindCollect() {
        if (this.data.slipStatus === false) {
            this.setData({
                currentId: this.data.receiveCurrent
            })
        } else {
            this.setData({
                currentId: String(this.data.slipCurrent)
            })
        }
        wx.cloud.callFunction({
            name: 'getVideoFun',
            data: {
                id: this.data.currentId,
                fun_name: 'get_id_video_detail',
                collection_name: 'videoData'
            },
            success: res => {
                let onRes = res.result.data[0];
                if (this.data.collectText == '收藏') {
                    this.setData({
                        collectText: '已收藏'
                    })
                    this.collectedAdd(onRes);
                } else {
                    this.setData({
                        collectText: '收藏'
                    })
                    this.collectedDelete(onRes);
                }
            }, fail: err => {
                util.errorModal(err);
            }
        })
    },
    collectedDelete(onResData) {
        wx.cloud.callFunction({
            name: 'deleteVideoFun',
            data: {
                fun_name: 'delete_id_video',
                collection_name: 'collectData',
                params: {
                    open_id: app.globalData.userInfo.openid,
                    id: onResData.video_id,
                }
            },
            fail: err => {
                util.errorModal(err);
            }
        })
    },
    collectedAdd(onResData) {
        wx.cloud.callFunction({
            name: 'addVideoFun',
            data: {
                fun_name: "add_collect_video",
                collection_name: "collectData",
                params: {
                    open_id: app.globalData.userInfo.openid,
                    id: onResData.video_id,
                    url: onResData.url,
                    title: onResData.title,
                    poster: onResData.poster,
                    look_num: onResData.look_num,
                }
            },
        }).catch(err => {
            util.errorModal(err);
        })
    },
    historyAdd() {
        if (this.data.slipStatus === false) {
            this.setData({
                currentId: this.data.receiveCurrent
            })
        } else {
            this.setData({
                currentId: String(this.data.slipCurrent)
            })
        }
        wx.cloud.callFunction({
            name: 'getVideoFun',
            data: {
                id: this.data.currentId,
                fun_name: 'get_id_video_detail',
                collection_name: 'videoData'
            },
            success: res => {
                let onRes = res.result.data[0];
                this.decideHistoryUserId(onRes);
            }, fail: err => {
                util.errorModal(err);
            }
        })
    },
    decideHistoryUserId(onResData) {
        wx.cloud.callFunction({
            name: "getVideoFun",
            data: {
                fun_name: 'get_openid_and_videoid_video_list',
                collection_name: "watchedData",
                open_id: app.globalData.userInfo.openid,
                id: onResData.video_id
            },
            success: res => {
                console.log('res', res);
                if (res.result.data.length <= 0) {
                    this.historyIdDetailAdd(onResData)
                }
            },
            fail: err => {
                util.errorModal(err);
            }
        });
    },
    historyIdDetailAdd(onResData) {
        wx.cloud.callFunction({
            name: 'addVideoFun',
            data: {
                fun_name: "add_collect_video",
                collection_name: "watchedData",
                params: {
                    open_id: app.globalData.userInfo.openid,
                    id: onResData.video_id,
                    url: onResData.url,
                    title: onResData.title,
                    poster: onResData.poster,
                    look_num: onResData.look_num,
                    watch_time: util.formatDate(Date.parse(new Date()))
                }
            },
        }).catch(err => {
            util.errorModal(err);
        })
    },
    decideIsUserCollect(onResData) {
        console.log('onResData', onResData)
        wx.cloud.callFunction({
            name: "getVideoFun",
            data: {
                fun_name: 'get_openid_and_videoid_video_list',
                collection_name: "collectData",
                open_id: app.globalData.userInfo.openid,
                id: onResData.video_id
            },
            success: res => {
                if (res.result.data.length > 0) {
                    this.setData({
                        collectText: '已收藏'
                    })
                } else {
                    this.setData({
                        collectText: '收藏'
                    })
                }
            },
            fail: err => {
                util.errorModal(err);
            }
        });
    },
    decideEveryOneVideoStatus() {
        if (this.data.slipStatus === false) {
            this.setData({
                currentId: this.data.receiveCurrent
            })
        } else {
            this.setData({
                currentId: String(this.data.slipCurrent)
            })
        }
        wx.cloud.callFunction({
            name: 'getVideoFun',
            data: {
                id: this.data.currentId,
                fun_name: 'get_id_video_detail',
                collection_name: 'videoData'
            },
            success: res => {
                let onRes = res.result.data[0];
                this.decideIsUserCollect(onRes);
            }, fail: err => {
                util.errorModal(err);
            }
        })
    },
    onShareAppMessage: function () {
        let shareVideoCurrent = null;
        if (this.data.slipStatus === false) {
            shareVideoCurrent = this.data.receiveCurrent
        } else {
            shareVideoCurrent = this.data.slipCurrent
        }
        return {
            title: `好友分享给你一条视频,注意查看哟！！！`,
            path: `/pages/playing/playing?nowCurrentId=${shareVideoCurrent}&openid=${app.globalData.userInfo.openid}`,
            imageUrl: this.data.videoList[shareVideoCurrent].poster
        }
    },
})